package com.weiiew.calculationService.service;

import com.example.feignapi.client.ItemClient;
import com.example.feignapi.client.RecipeClient;
import com.weiiew.calculationService.craft.CraftCalculator;
import com.weiiew.calculationService.craft.CraftCalculatorImpl;
import com.weiiew.calculationService.element.elGraph;
import com.weiiew.common.entity.factory.Factory;
import com.weiiew.common.exception.LockItemException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description 进行配方的计算
 * @Author 魏锴
 * @Date 2024/11/17
 */
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class CalculationServiceImpl implements CalculationService {
    private final RecipeClient recipeService;
    private final ItemClient itemService;

    /**
     * 根据不同版本对应的数据转发计算请求
     * @param factory 工厂信息
     * @param version 版本信息
     * @return 可以直接使用的图结构
     * @throws LockItemException 缺失物品异常
     */
    @Override
    public elGraph calculation(Factory factory, int version)
            throws LockItemException {
        CraftCalculator craftCalculator = getCraftCalculator(version);
        if (craftCalculator == null) return null;
        craftCalculator.setItemService(itemService);
        craftCalculator.setRecipeService(recipeService);
        return craftCalculator.calculation(factory, version);
    }
    private CraftCalculator getCraftCalculator(int version){
        if (version > 0) return new CraftCalculatorImpl();
        else return null;
    }
}
